package com.github.prontera.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.common.filter.IgnoreAuth;
import com.github.prontera.domain.ClientTaskCount;
import com.github.prontera.domain.Task;
import com.github.prontera.domain.enums.TaskTypeEnum;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 任务广场
 */
@RestController
@Api(value = "任务广场")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/task")
public class TaskController{

	@Autowired
	private TaskService taskService;
	
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	@IgnoreAuth
	@ApiOperation(value = "任务广场" , notes = "任务广场")
	public ObjectDataResponse<List<Task>> find(@RequestBody Task task){
		task.setStatus(0);
		task.setDisplay(0);
		task.setOrderBy("T_TASK.create_time desc");
		List<Task> datas=taskService.find(task);
		return new ObjectDataResponse<List<Task>>(datas);
	}
	
	@RequestMapping(value = "findClientTask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "我的任务广场" , notes = "我的任务广场")
	public ObjectDataResponse<List<Task>> findClientTask(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Task task){
		task.setClientId(clientId);
		task.setOrderBy("T_CLIENT_TASK.create_time desc");
		List<Task> datas=taskService.findClientTask(task);
		System.out.println(datas);
		return new ObjectDataResponse<List<Task>>(datas);
	}
	
	@RequestMapping(value = "cancelClientTask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "撤销任务" , notes = "撤销任务")
	public ObjectDataResponse<Boolean> cancelClientTask(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Task task){
		if(task.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"参数错误!");
		}
		if(taskService.cancelClientTask(task.getId(),clientId)){
			return new ObjectDataResponse<Boolean>(true);
		}
		return new ObjectDataResponse<Boolean>(false);
	}
	
	@RequestMapping(value = "getTask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查看单个任务" , notes = "查看单个任务")
	public ObjectDataResponse<Task> getTask(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Task task){
		if(task.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"参数错误!");
		}
		Task taskData=taskService.get(task.getId());
		return new ObjectDataResponse<Task>(taskData);
	}
	
	@RequestMapping(value = "getClientTask", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "查看当前用户单个任务" , notes = "查看当前用户单个任务")
	public ObjectDataResponse<Task> getClientTask(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Task task){
		if(task.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"参数错误!");
		}
		task.setClientId(clientId);
		Task taskData=taskService.getClientTask(task);
		if(taskData!=null){
			Map<Integer,String> taskRemark=new HashMap<Integer,String>();
			ClientTaskCount clientTaskCountObj=taskData.getClientTaskCount();
			
			String[] claimTypes= taskData.getClaimTypes().split("[,]");
			String[] claimValues= taskData.getClaimValues().split("[,]");
			for (int i = 0; i < claimTypes.length; i++) {
				String claimType=claimTypes[i];
				String claimValue=claimValues[i];
				
				TaskTypeEnum taskTypeEnum=TaskTypeEnum.getEnumByCode(Integer.parseInt(claimType));
				if(taskTypeEnum==TaskTypeEnum.ONE){
					taskRemark.put(TaskTypeEnum.ONE.getCode(),"已经连续签到"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"天");
	  			}else if(taskTypeEnum==TaskTypeEnum.TWO){
	  				taskRemark.put(TaskTypeEnum.TWO.getCode(),"已经连续每天查看"+clientTaskCountObj.getVideoCount()+"/"+claimValue+"达人视频");
	  			}else if(taskTypeEnum==TaskTypeEnum.THREE){
	  				taskRemark.put(TaskTypeEnum.THREE.getCode(),"已经累计邀请"+clientTaskCountObj.getInvitationCount()+"/"+claimValue+"好友");
				}else if(taskTypeEnum==TaskTypeEnum.FOUR ){
					taskRemark.put(TaskTypeEnum.FOUR.getCode(),"已经累计邀请"+clientTaskCountObj.getConsumerCount()+"/"+claimValue+"消费用户");
				}else if(taskTypeEnum==TaskTypeEnum.FIVE){
					taskRemark.put(TaskTypeEnum.FIVE.getCode(),"已经累计完成"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"消费返利订单");
				}else if(taskTypeEnum==TaskTypeEnum.SIX){ 
					taskRemark.put(TaskTypeEnum.SIX.getCode(),"已经累计完成邀请"+clientTaskCountObj.getDaySigned()+"/"+claimValue+"名技能达人");
				}
			}
			taskData.setObtainTime(taskData.getClientTask().getCreateTime());
			taskData.setStatus(taskData.getClientTask().getStatus());
			taskData.setTaskRemark(taskRemark);
			taskData.setClientTaskCount(null);
			taskData.setClientTask(null);
		}
		return new ObjectDataResponse<Task>(taskData);
	}
	
	
}
